package com.study.happystudy.tool;

import cn.hutool.json.JSONUtil;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.cn;

/**
 * 阿里云的短信验证工具类
 */
@Component
public class SmsUtil{

    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.sms.templateCode}")
    private String templateCode;

    private AsyncClient getSmsClient() {
        // Configure Credentials authentication information, including ak, secret, token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                //.securityToken("<your-token>") // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
        return client;
    }

    public void sendSms(String phone, HashMap<String, Object> templateParam) {

        System.out.println(accessKeyId+" "+accessKeySecret+" "+templateCode);
        try{
            AsyncClient client = getSmsClient();

            // Parameter settings for API request
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName("悦学乐思")
                    .templateCode(templateCode)
                    .phoneNumbers(phone)
                    .templateParam(JSONUtil.toJsonStr(templateParam))
                    .build();

            // Asynchronously get the return value of the API request
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            // Synchronously get the return value of the API request
            SendSmsResponse resp = response.get();
//        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
//        System.out.println(response);
            System.out.println(resp);
            // Finally, close the client
            client.close();
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
